/*
 * Created on Jan 12, 2009
 */
package ak.salmon.model;

import java.awt.Point;

import ak.salmon.markup.DrawingPalette;

public class Node implements DrawingElement {

    private static final int MIN_WIDTH = 160;
    private static final int PADDING = 10;
    private static final int LINE_HEIGHT = 15;
    private static final int AVG_CHAR_WIDTH = 6;

    private int width;
    private int height;
    private Point absolutePosition;
    private Point relativePosition = new Point(0, 0);

    private final NodeContent content;

    public Node(NodeContent content) {
        this.content = content;
    }

    @Override
    public void plan() {

        this.width = MIN_WIDTH;
        for (int i = 0; i < content.getNumberOfLines(); i++) {
            String line = content.getLine(i);
            int lineWidth = AVG_CHAR_WIDTH * line.length();
            if (lineWidth > width) width = lineWidth;
        }

        int lines = content.getNumberOfLines();
        this.height = 2 * PADDING + lines * LINE_HEIGHT + LINE_HEIGHT / 2;
    }

    @Override
    public void position(Point point) {
        this.absolutePosition = point;
    }

    @Override
    public void draw(DrawingPalette palette) {

        int rectX = absolutePosition.x + PADDING;
        int rectY = absolutePosition.y + PADDING;
        int rectWidth = width - 2 * PADDING;
        int rectHeight = height - 2 * PADDING;

        palette.drawBox(rectX, rectY, rectWidth, rectHeight);
        palette.drawText(getMiddleX(), rectY, content);
    }

    private int getMiddleX() {
        return absolutePosition.x + width / 2;
    }

    private int getMiddleY() {
        return absolutePosition.y + height / 2;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public int getChildrenHeight() {
        return 0;
    }

    @Override
    public int getChildrenWidth() {
        return 0;
    }

    @Override
    public Point getTopHub() {
        return new Point(getMiddleX(), absolutePosition.y + PADDING);
    }

    @Override
    public Point getLeftHub() {
        return new Point(absolutePosition.x + PADDING, getMiddleY());
    }

    @Override
    public Point getRightHub() {
        return new Point(absolutePosition.x + width - PADDING, getMiddleY());
    }

    @Override
    public Point getBottomHub() {
        return new Point(getMiddleX(), absolutePosition.y + height - PADDING);
    }

    @Override
    public Point getRelativePosition() {
        return relativePosition;
    }

    @Override
    public void setRelativePosition(Point relativePosition) {
        this.relativePosition = relativePosition;
    }

    public Object getId() {
        return content.getId();
    }

    public Object getParentId() {
        return content.getParentId();
    }
}
